using System.IO;
using System.Linq;
using QFramework.Framework.ResKit.HotUpdate;
using UnityEditor;
using UnityEngine;

namespace QFramework.Framework.ResKit.Editor
{
    public static class AssetBundleExporter
    {
#if UNITY_EDITOR
        [MenuItem("QFramework/Framework/ResKit/Build AssetBundles", false)]
        private static void BuildAssetBundles()
        {
            var outputPath = Application.streamingAssetsPath + "/AssetBundles/" + ResKitUtil.GetPlatformName();

            if (!Directory.Exists(outputPath)) Directory.CreateDirectory(outputPath);

            BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.ChunkBasedCompression,
                EditorUserBuildSettings.activeBuildTarget);

            var versionConfigFilePath = outputPath + "/ResVersion.json";
            var resVersion = new ResVersion
            {
                version = 15,
                assetBundleNames = AssetDatabase.GetAllAssetBundleNames().ToList()
            };

            var resVersionJson = JsonUtility.ToJson(resVersion, true);
            File.WriteAllText(versionConfigFilePath, resVersionJson);

            AssetDatabase.Refresh();
        }
#endif
    }
}